package com.wechat.weibo.service;

import com.wechat.weibo.dao.PickDao;
import com.wechat.weibo.dao.UserDao;
import com.wechat.weibo.entity.User;
import com.wechat.weibo.entity.Follows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class ShowService {
    final int SELF_IS_FAN=1;
    final int SELF_IS_MAIN=0;
    @Autowired(required = false)
    PickDao pickDao;

    @Autowired(required = false)
    UserDao userDao;

    public User judge(Follows follows,int mode){
        User user;
        if(mode==SELF_IS_FAN)user=userDao.getUser(follows.getMain_id());
        else user=userDao.getUser(follows.getFan_id());
        if(user==null)return null;
        if(follows.isFan_pick_main()&&follows.isPick_back())user.setPick_mode(1);
        else if(!(follows.isFan_pick_main()||follows.isPick_back()))user.setPick_mode(-2);
        else {
            if(mode==SELF_IS_FAN){
                if(follows.isFan_pick_main())user.setPick_mode(0);
                else user.setPick_mode(-1);
            }
            else {
                if(follows.isFan_pick_main())user.setPick_mode(-1);
                else user.setPick_mode(0);
            }
        }
        return user;
    }
    public User showRelation(String selfID, String otherID){
        User user=userDao.getUser(otherID);
        if(user==null)return null;
        if(selfID.equals(otherID)){
            user.setPick_mode(-3);
            return user;
        }
        Follows follows=pickDao.showRelation(selfID,otherID);
        if(follows==null){
            user.setPick_mode(-2);
            return user;
        }
        return judge(follows,SELF_IS_FAN);
    }
    public List<User> showSelfPick(String openid){
        List<Follows> list=pickDao.getPick(openid);
        List<User> res=new LinkedList<>();
        for (Follows p:
             list) {
            User user=judge(p,SELF_IS_FAN);
            if(user==null)continue;
            res.add(user);
        }
        return res;
    }
    public List<User> showSelfFans(String openid){
        List<Follows> list=pickDao.getFans(openid);
        List<User> res=new LinkedList<>();
        for (Follows p:
                list) {
            User user=judge(p,SELF_IS_MAIN);
            if(user==null)continue;
            res.add(user);
        }
        return res;
    }
    public List<User> showOtherPick(String selfID, String otherID){
        List<Follows> list=pickDao.getPick(otherID);
        List<User> res=new LinkedList<>();
        for (Follows p:list
             ) {
            if(selfID.equals(p.getMain_id()))continue;
            Follows tmp=pickDao.showRelation(selfID,p.getMain_id());
            User user;
            if(tmp==null){
                user=userDao.getUser(p.getMain_id());
                if(user==null)continue;
                user.setPick_mode(-2);
            }
            else {
//                System.out.print(tmp);
                user=judge(tmp,SELF_IS_FAN);
                if(user==null)continue;
            }
            res.add(user);
        }
        return res;
    }
    public List<User> showOtherFan(String selfID, String otherID){
        List<Follows> list=pickDao.getFans(otherID);
        List<User> res=new LinkedList<>();
        for (Follows p:list
        ) {
            if(selfID.equals(p.getFan_id()))continue;
            Follows tmp=pickDao.showRelation(selfID,p.getFan_id());
            User user;
            if(tmp==null){
                user=userDao.getUser(p.getMain_id());
                if(user==null)continue;
                user.setPick_mode(-2);
            }
            else {
                user=judge(tmp,SELF_IS_FAN);
                if(user==null)continue;
            }
            res.add(user);
        }
        return res;
    }
}
